package cn.wizzer.app.web.modules.controllers.open;

import cn.wizzer.app.web.commons.constant.ApiConstant;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.base.Result;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.impl.AdaptorErrorContext;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Wizzer on 2016/7/5.
 */
@IocBean
@At("/open/api/file/upload")
public class ApiUploadController {
    private static final Log log = Logs.get();


    /**
     * 上传到本地
     *
     * @param tf
     * @param err
     * @return
     */
    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
    @At
    @Ok("json")
    @ULog(tag = "用户文件上传到本地")
    public Object image(@Param("Filedata") TempFile tf, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return Result.error("文件不合法");
            } else if (tf == null) {
                return Result.error("空文件");
            } else {
                String uri = "/image/" + DateFormatUtils.format(new Date(), "yyyyMMdd", Locale.CHINA)+ "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                String f = ApiConstant.APP_UPLOAD_BASE + uri ;
                Files.write(new File(f), tf.getInputStream());
                log.info("文件上传到本地, 操作用户: " + Mvcs.getReq().getHeader("appid") +
                        ", 文件名: " + tf.getName());
                return Result.success("上传成功", uri);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("系统错误");
        } catch (Throwable e) {
            return Result.error("文件格式错误");
        }
    }

    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
    @At
    @Ok("json")
    @ULog(tag = "用户文件上传到本地2")
    public Object image2(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (tf == null) {
                return Result.error("空文件");
            } else {
                String uri = "/image/" + DateFormatUtils.format(new Date(), "yyyyMMdd", Locale.CHINA)+ "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                String f = ApiConstant.APP_UPLOAD_BASE + uri ;
                Files.write(new File(f), tf.getInputStream());
                log.info("文件上传到本地, 操作用户: " + Mvcs.getReq().getHeader("appid") +
                        ", 文件名: " + tf.getName());
                return Result.success("上传成功", uri);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("系统错误");
        } catch (Throwable e) {
            return Result.error("图片格式错误");
        }
    }
}
